﻿/*
* Copyright 2009-2010 AKABANA.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
* either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
using System;
using System.Globalization;
using System.Reflection;

namespace Akabana.Amf.Util
{
    public static class PropertyUtil
    {
        private static Binder binder = new PropertyBinder();

        public static void BindProperties(object value, string[] fieldNames, object[] fieldValues)
        {
            var type = value.GetType();
            var fieldLength = fieldNames.Length;
            for (var i = 0; i < fieldLength; i++)
            {
                var fieldName = fieldNames[i];
                var fieldValue = fieldValues[i];

                try
                {
                    var field = type.GetField(fieldName);
                    if (field != null)
                    {
                        field
                            .SetValue(
                                value,
                                fieldValue,
                                BindingFlags.Public | BindingFlags.Instance,
                                binder,
                                CultureInfo.CurrentCulture
                            );
                        continue;
                    }
                    var property = type.GetProperty(fieldName);
                    if (property != null && property.CanWrite)
                    {
                        property
                            .SetValue(
                                value,
                                fieldValue,
                                BindingFlags.Public | BindingFlags.Instance,
                                binder,
                                null,
                                CultureInfo.CurrentCulture
                            );
                        continue;
                    }
                }
                catch (System.Exception e)
                {
                    throw new System.Exception(value.GetType().FullName + "#" + fieldName + "=" + fieldValue, e);
                }
            }
        }

        public static void Copy(object src, object dest)
        {

            var srcType = src.GetType();
            var destType = dest.GetType();
            var srcProps = srcType.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (var srcProp in srcProps)
            {
                var destProp = destType.GetProperty(srcProp.Name, BindingFlags.Public | BindingFlags.Instance);
                if (destProp != null && destProp.CanWrite)
                {
                    destProp
                        .SetValue(
                            dest,
                            srcProp.GetValue(src, null),
                            BindingFlags.Public | BindingFlags.Instance,
                            binder,
                            null,
                            CultureInfo.CurrentCulture
                        );
                }
                var destField = destType.GetField(srcProp.Name, BindingFlags.Public | BindingFlags.Instance);
                if (destField != null)
                {
                    destField.SetValue(
                            dest,
                            srcProp.GetValue(src, null),
                            BindingFlags.Public | BindingFlags.Instance,
                            binder,
                            CultureInfo.CurrentCulture
                            );
                }
            }

            var srcFields = srcType.GetFields(BindingFlags.Public | BindingFlags.Instance);

            foreach (var srcField in srcFields)
            {
                var destProp = destType.GetProperty(srcField.Name, BindingFlags.Public | BindingFlags.Instance);
                if (destProp != null && destProp.CanWrite)
                {
                    destProp
                        .SetValue(
                            dest,
                            srcField.GetValue(src),
                            BindingFlags.Public | BindingFlags.Instance,
                            binder,
                            null,
                            CultureInfo.CurrentCulture
                        );
                }
                var destField = destType.GetField(srcField.Name, BindingFlags.Public | BindingFlags.Instance);
                if (destField != null)
                {
                    destField.SetValue(
                            dest,
                            srcField.GetValue(src),
                            BindingFlags.Public | BindingFlags.Instance,
                            binder,
                            CultureInfo.CurrentCulture
                            );
                }
            }
        }
    }


    internal class PropertyBinder : Binder
    {

        public override PropertyInfo SelectProperty(BindingFlags bindingAttr, PropertyInfo[] match, Type returnType, Type[] types, ParameterModifier[] modifiers)
        {
            throw new System.Exception("no implementation");            
        }

        public override FieldInfo BindToField(BindingFlags bindingAttr, FieldInfo[] match, object value, CultureInfo culture)
        {
            throw new System.Exception("no implementation");
        }

        public override MethodBase BindToMethod(BindingFlags bindingAttr, MethodBase[] match, ref object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] names, out object state)
        {
            throw new System.Exception("no implementation");
        }

        public override void ReorderArgumentArray(ref object[] args, object state)
        {
            throw new System.Exception("no implementation");
        }

        public override object ChangeType(object value, Type changeType, CultureInfo culture)
        {
            return TypeUtil.ChangeType(value, changeType, culture);
        }

        public override MethodBase SelectMethod(BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers)
        {
            throw new System.Exception("no implementation");
        }
    }
}